import numpy as np
import bokeh
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point, Polygon
import json
from bokeh.io import curdoc, output_notebook
from bokeh.models import Slider, HoverTool
from bokeh.layouts import widgetbox, row, column
from bokeh.io import output_notebook, show, output_file
from bokeh.plotting import figure
from bokeh.models import GeoJSONDataSource, LinearColorMapper, ColorBar
from bokeh.palettes import brewer
from bokeh.plotting import ColumnDataSource, figure, output_file, show
from shapely.geometry import Point, Polygon
shapefile = '../data/syr_admin_json_190321.json'
gdf = gpd.read_file(shapefile)
gdf['id'] = gdf.index
gdf.set_index('id', drop=True, inplace=True)
gdf.head()
df = pd.read_csv('../data/syrian_war.csv')
df.head()
ngdf = gdf.drop(['NAM_EN_REF', 'NAM_AR_REF', 'PCODE', 'ADM2_EN',
'ADM2_AR', 'ADM2_PCODE', 'ADM1_EN', 'ADM1_AR', 'ADM1_PCODE', 'ADM0_EN',
'ADM0_AR', 'ADM0_PCODE', 'UPDATE_DAT'], axis=1)
ngdf.head()
ngdf['fatalities'] = ngdf.apply(lambda row:np.sum(df[df['region_id']==row.name]['fatalities']), axis=1)
ngdf.head()
centers = list(ngdf.apply(lambda row: Polygon(row['geometry']).centroid, axis=1))
ngdf['center_x'] = [c.x for c in centers]
ngdf['center_y'] = [c.y for c in centers]
ngdf.head()
np.max(ngdf['fatalities']),np.mean(ngdf['fatalities'])
ngdf['fatalities'].hist()
json_data = ngdf.to_json()
geosource = GeoJSONDataSource(geojson = json_data)
palette = brewer['YlGnBu'][8][::-1]
max_value = np.max(ngdf['fatalities'])
color_mapper = LinearColorMapper(palette=palette, low=0, high=max_value)
tick_labels = {f'{i}': f'{i}' for i in range(0,max_value,1000)}
color_bar = ColorBar(color_mapper=color_mapper,
label_standoff=8,
width = 500, height = 20,
border_line_color=None,
location = (0,0),
orientation = 'horizontal',
major_label_overrides = tick_labels)
p = figure(title = 'Syria Map', plot_height = 1000 , plot_width = 1000, toolbar_location = None)
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
p.patches(source = geosource,
fill_color = {'field' :'fatalities', 'transform' : color_mapper},
line_color = 'black', line_width = 0.25, fill_alpha = 0.8)
p.add_layout(color_bar, 'below')
output_notebook()
show(p)
ngdf[['NAME_EN','fatalities','center_x', 'center_y']].head()
max_value = np.max(ngdf['fatalities'])
color_mapper = LinearColorMapper(palette=palette, low=0, high=max_value)
TOOLTIPS = [
("region", "@NAME_EN"),
("fatalities", "@fatalities"),
]
tick_labels = {f'{i}': f'{i}' for i in range(0, max_value, 1000)}
color_bar = ColorBar(color_mapper=color_mapper,
label_standoff=8,
width = 500, height = 20,
border_line_color=None,
location = (0,0),
orientation = 'horizontal',
major_label_overrides = tick_labels)
p = figure(title = 'Syria Map', plot_height=800 , plot_width=800, tooltips=TOOLTIPS)
p.patches(source=geosource,
fill_color={'field' : 'fatalities', 'transform' : color_mapper},
line_color='black', line_width=0.25, fill_alpha=0.8)
p.add_layout(color_bar, 'below')
output_notebook()
show(p)